package practise.alogrithm.hundredInterviewQuestions.secondTime;


import practise.alogrithm.hundredInterviewQuestions.Utils.LinkedListBuilder;
import practise.alogrithm.hundredInterviewQuestions.Utils.ListNode;
import practise.alogrithm.hundredInterviewQuestions.Utils.TreeNode;

/**
 * Created by Richard on 7/26/2014.
 */
public class LastKthNumberInLinkedList {
    public Integer get(ListNode node, Integer index ){
        ListNode backNode = node;
        boolean found = false;
       for(int i=0; node.getNext()!=null; i++,node=node.getNext()){

           if(i>=index){
               backNode = backNode.getNext();
               found=true;
           }
       }
        if(!found){
            throw new IndexOutOfBoundsException();
        }
        return backNode.getValue();
    }

    public static void main(String[] args){
        LastKthNumberInLinkedList lastKthNumberInLinkedList = new LastKthNumberInLinkedList();
        System.out.print(lastKthNumberInLinkedList.get(LinkedListBuilder.buildLinkedListFromArray(new int[]{1,2,3,4,5,6,7}),3));
    }
}
